package server

import (
	"os"
	"strconv"
	"tsugi/conf"
	"tsugi/mqs/rabbitmq"
)

func Locate(name string) bool {
	_, e := os.Stat(name)

	return !os.IsNotExist(e)
}

func StartLocate() {
	con := conf.StorageRoot()
	q := rabbitmq.New(con.Server.RabbitServer)
	q.Bind("dataServer")
	consume := q.Consume()
	for msg := range consume {
		object, err := strconv.Unquote(string(msg.Body))
		if err != nil {
			panic(err)
		}
		if Locate(con.Server.Dir + "/object/" + object) {
			q.Send(msg.ReplyTo, con.Server.Listen)
		}
	}
}
